<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class matakuliah extends CI_Controller {
	
	function __construct(){
		parent::__construct();
		$this->load->model('mdl_matakuliah');
		$this->load->library('Excel_generator');
		//$this->output->enable_profiler(TRUE);
	}
	
	function index(){
		$this->load->view('matakuliah');
	}
	
	function grid(){
		$data = $this->mdl_matakuliah->getdata();
		echo $this->mdl_matakuliah->togrid($data['row_data'], $data['row_count']);
	}
	
	function add(){
		$data['kode'] = '';
		$data['kd_prodi'] = '';
		$data['kd_fakultas'] = '';
		$data['kd_kurikulum'] = '';
		$data['kd_jenismk'] = '';
		$data['kd_kelompokmk'] = '';
		$data['kd_matakuliah'] = '';
		$data['nama_matakuliah'] = '';
		$data['sks'] = '';
		$data['semester'] = '';
		$data['mk_prasyarat'] = '';
		$data['ditawarkan'] = '';
		$data['status'] = 'A';
		$this->load->view('matakuliah_form', $data);
	}
	
	function edit($kode){
		$r = $this->mdl_matakuliah->getdataedit($kode);
		//echo($kode);
		$data['kode'] = $kode;
		$data['id_matakuliah']=$r->row()->id_matakuliah;
		$data['kd_prodi'] = $r->row()->kd_prodi;
		$data['kd_fakultas'] = $r->row()->kd_fakultas;
		$data['kd_kurikulum'] = $r->row()->kd_kurikulum;
		$data['kd_jenismk'] = $r->row()->kd_jenismk;
		$data['kd_kelompokmk'] = $r->row()->kd_kelompokmk;
		$data['kd_matakuliah'] = $r->row()->kd_matakuliah;
		$data['nama_matakuliah'] = $r->row()->nama_matakuliah;
		$data['sks'] = $r->row()->sks;
		$data['semester'] = $r->row()->semester;
		$data['mk_prasyarat'] = $r->row()->mk_prasyarat;
		$data['ditawarkan'] = $r->row()->ditawarkan;
		$data['status'] = $r->row()->status;
		$this->load->view('matakuliah_form', $data);
	}
	
	function save($aksi){
		# init
		$status = "";
		$result = false;
		$data['pesan_error'] = '';
		
		# get post data
		foreach($_POST as $key => $value){
			$data[$key] = $value;
		}
		
		# rules validasi form
		$this->form_validation->set_rules("kd_fakultas", 'Fakultas', 'trim|required|xss_clean');
		$this->form_validation->set_rules("kd_prodi", 'Prodi', 'trim|required|xss_clean');
		$this->form_validation->set_rules("kd_kurikulum", 'Kode Kurikulum', 'trim|required|xss_clean');
		$this->form_validation->set_rules("kd_jenismk", 'Jenis MK', 'trim|required|xss_clean');
		$this->form_validation->set_rules("kd_kelompokmk", 'Kelompok MK', 'trim|required|xss_clean');
		$this->form_validation->set_rules("kd_matakuliah", 'Kode Mata Kuliah', 'trim|required|xss_clean');
		$this->form_validation->set_rules("nama_matakuliah", 'Nama Mata Kuliah', 'trim|required|xss_clean');
		$this->form_validation->set_rules("sks", 'SKS', 'trim|required|numeric|xss_clean');
		$this->form_validation->set_rules("semester", 'Semester', 'trim|required|xss_clean');
		$this->form_validation->set_rules("status", 'Status', 'trim|required|xss_clean');
		
		
		# message rules
		$this->form_validation->set_message('required', 'Field %s harus diisi.');

		$data['pesan_error'] = '';
		if ($this->form_validation->run() == FALSE){
			//$data["pesan_error"] .= trim(form_error('kd_matakuliah',' ',' '))==''?'':form_error('kd_matakuliah',' ',' ').'<br>';
			$data["pesan_error"] .= trim(validation_errors(' ',' '))==''?'':validation_errors(' ',' ');
		}else{
			if($aksi=="add"){ // add
				$result = !$this->mdl_matakuliah->isExistKode($data['kd_matakuliah']);
				if ($result){
					$result = $this->mdl_matakuliah->InsertOnDb($data);
				}else{
					$data['pesan_error'] .= 'Sudah terdapat kode dalam database!';
				}
			}else { // edit
				if($data['kode'] == $data['kode']){ // jika kode tidak diubah
					$result=$this->mdl_matakuliah->UpdateOnDb($data);
				}else{
					$result = !$this->mdl_matakuliah->isExistKode($data['kode']);
					if ($result){
						$result=$this->mdl_matakuliah->UpdateOnDb($data);
					}else{
						$data['pesan_error'] .= 'Sudah terdapat kode dalam database!';
					}
				}
			}
		}
		
		if($result){
			echo json_encode(array('success'=>true));
		}else{
			echo json_encode(array('msg'=>$data['pesan_error']));
		}
	}
	
	function delete($kode){
		$result = $this->mdl_matakuliah->DeleteOnDb($kode);
		if ($result){
			echo json_encode(array('success'=>true));
		} else {
			echo json_encode(array('msg'=>'Data gagal dihapus'));
		}
	}
	
	
	function addsap($id){
		// get data
		$label = $this->mdl_matakuliah->getdataedit($id);
		$intro = $this->mdl_matakuliah->getIntro($label->row()->kd_prodi, $label->row()->kd_matakuliah);
		$sap   = $this->mdl_matakuliah->getSap($label->row()->kd_prodi, $label->row()->kd_matakuliah);
		
		# hidden input
		$data['kd_matakuliah'] = $id;
		$data['kd_prodi'] = $label->row()->kd_prodi;
		$data['nama_matakuliah'] = $label->row()->nama_matakuliah;
		$data['nama_prodi'] = $label->row()->nama_prodi;
		# data input intro
		if($intro->num_rows() > 0){
			$data['deskripsi'] = $intro->row()->deskripsi;
			$data['kompetensi'] = $intro->row()->kompetensi;
			$data['daftar_pustaka'] = $intro->row()->daftar_pustaka;
			$data['kompetensi_dosen'] = $intro->row()->kompetensi_dosen;
		}else{
			$data['deskripsi'] = '';
			$data['kompetensi'] = '';
			$data['daftar_pustaka'] = '';
			$data['kompetensi_dosen'] = '';
		}
		# data input sap
		$sap_row = $sap->num_rows();
		if($sap_row > 0){
			$data['data_sap'] = json_encode($sap->result_array()); //$this->mdl_matakuliah->togrid($sap, $sap_row);
		}else{
			$data['data_sap'] = '['.json_encode(array(
										'kd_prodi'=>'',
										'kd_matakuliah'=>'',
										'kd_materi'=>'',
										'Materi'=>'',
										'Waktu'=>'',
										'Metode'=>'',
									)).']';
		}
		
		$this->load->view('matakuliah_form_sap', $data);
		
	}
	
	function save_sap(){
		# get post data
		foreach($_POST as $key => $value){
			$data[$key] = $value;
		}
		
		# init
		$status = "";
		$result = false;
		$data['pesan_error'] = '';
		
		$result = $this->mdl_matakuliah->InsertSAP($data);
		
		if($result){
			echo json_encode(array('success'=>true));
		}else{
			echo json_encode(array('msg'=>$data['pesan_error']));
		}
	}
	
	function delete_sap($kd_prodi, $kd_matakuliah, $kd_materi){
		# init
		$result = false;
		$data['pesan_error'] = '';
		
		$result = $this->mdl_matakuliah->DeleteSAP($kd_prodi, $kd_matakuliah, $kd_materi);
		
		if($result){
			echo json_encode(array('success'=>true));
		}else{
			echo json_encode(array('msg'=>$data['pesan_error']));
		}
	}
	
	/*function pdf(){
		# inisialisasi library
		$this->load->library('our_pdf');
		$this->our_pdf->FPDF('L', 'mm', 'A4');
		define('FPDF_FONTPATH',APPPATH."libraries/fpdf/font/");
		
		// ambil data dari tabel
		$pdfdata = $this->mdl_matakuliah->get_pdf();
		if (count($pdfdata['row'])==0){
			echo "Data Tidak Tersedia";
			return;
		}
		
		# create pdf
		$this->our_pdf->Open();
		$this->our_pdf->addPage();
		
		// judul laporan
		$this->our_pdf->setFont('arial','B',16);
		$posY = 35;
		$posX = 10;
		$midX = 150;
		$title = 'Data Matakuliah';
		$textPosX = $midX - ($this->our_pdf->GetStringWidth($title) / 2);
		$this->our_pdf->text($textPosX,$posY,$title);
		
		# setting coloumn
		$this->our_pdf->setFont('Arial','B',9);
		$this->our_pdf->setFillColor(255,255,255);
		$posY += 6;
		$this->our_pdf->setXY($posX,$posY);
		
		// init column
		$col_width = array();
		$col_align = array();
		$col_caption = array();
		$col_val_align = array();
		foreach($pdfdata['column'] as $col){
			array_push($col_width, 		$col['width']);
			array_push($col_align, 		$col['align']);
			array_push($col_caption, 	$col['caption']);
			array_push($col_val_align, 	$col['val_align']);
		}
		
		// set column option
		$this->our_pdf->SetWidths($col_width);
		$this->our_pdf->SetAligns($col_align);
		$this->our_pdf->Row($col_caption);
		
		# setting row
		$posY = 39;
		$this->our_pdf->setFont('arial','',9);	
		$this->our_pdf->setFillColor(255,255,255);
		
		$this->our_pdf->SetAligns($col_val_align);
		
		// insert record
		foreach($pdfdata['row'] as $row){
			$this->our_pdf->Row($row);
		}
		
		// finish
		$this->our_pdf->AliasNbPages();
		$this->our_pdf->Output("Data Matakuliah.pdf", "I");
	}*/
	
	function laporan_pdf($kd_prodi, $kd_kurikulum){
		 $this->load->library('HTML2PDF');
		 $html2pdf = new HTML2PDF('L', 'A4', 'fr');
		 $html2pdf->setDefaultFont('Arial');
		
		//filter
		#get filter
		$fil['kd_prodi'] = $kd_prodi;
		$fil['kd_kurikulum'] = $kd_kurikulum;
		
		 //$data['nama'] = 'indriyanto';
		 $data['namaUniv'] = 'UNIVERSITAS OTTOW GEISSLER';
		 $data['alamatUniv'] = 'Jln. Perkutut Kotaraja, Telp.(0967)581562';
		 $data['kotaUniv'] = 'Jayapura, Papua';
		 // ambil data dari tabel
		$data['data_pdf'] = $this->mdl_matakuliah->get_pdf($fil);
		
		
		/* if (count($da['row'])==0){
			echo "Data Tidak Tersedia";
			return;
		} */
		
		
		 $konten = $this->load->view('matakuliah_laporan',$data,true);
		
         $html2pdf->writeHTML($konten, false);
        
		 $html2pdf->Output('matakuliah.pdf');
	}
	
	function laporan_excel() {			
		$query = $this->db->get('ref_matakuliah');
			$this->excel_generator->set_query($query);
			$this->excel_generator->set_header(array('Kode Kurikulum', 'Kode Prodi', 'Kode Matakuliah', 'Nama Matakuliah', 'SKS', 'Semester', 'Kode Jenis MK', 'Kode kelompok MK', 'MK Prasyarat', 'Ditawarkan', 'Status'));
			$this->excel_generator->set_column(array('kd_kurikulum', 'kd_prodi', 'kd_matakuliah', 'nama_matakuliah', 'sks', 'semester', 'kd_jenismk', 'kd_kelompokmk', 'mk_prasyarat', 'ditawarkan', 'status'));
			$this->excel_generator->set_width(array(10, 10, 10, 30, 8, 10, 10, 10, 10, 10, 10));
			$this->excel_generator->exportTo2007('Matakuliah');
	}
	
}